#include "Widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QFileDialog>
#include <QDebug>
#include <QKeyEvent>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("Tank 20022");
    mypainter = new QPainter;
    gameMap = new GameMap(this);
    player1 = new Tank(this);
    timer=new QTimer(this);
    timer->start(100);
    connect(timer,&QTimer::timeout,[this](){update();});
    QString filename = QFileDialog::getOpenFileName(this,"选择地图文件","./","*.txt");
    gameMap->iniByTxt(filename);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::DrawMap()
{
    for(int i=0;i<gameMap->row;i++)
    {
        for(int j=0;j<gameMap->col;j++)
        {
            QString imageUrl;
            switch(gameMap->Gmap[i][j])
            {
            case road:imageUrl=":/map_image/element/road.png";break;
            case wall:imageUrl=":/map_image/element/wall.gif";break;
            case grass:imageUrl=":/map_image/element/grass.gif";break;
            }
            QImage image(imageUrl);
            mypainter->drawImage(QRectF(j*image.width(),i*image.height(),image.width(),image.height()),image);
        }
    }
}

void Widget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    mypainter->begin(this);
    DrawMap();
    player1->DrawTank(mypainter,player1->mydir);

    mypainter->end();
}

bool Widget::iscollide(int dx, int dy)
{
    QImage mywall(":/map_image/element/wall.gif");
    QImage mytank(":/map_image/element/tank.png");
    int newx=player1->pos_x+dx;
    int newy=player1->pos_y+dy;
    if(newx>=(gameMap->col-1)*mywall.width() || newy>=(gameMap->row-1)*mywall.height() || newx<=0 || newy <=0)
    {
        qDebug()<<"坦克试图越界";
        return true;
    }
    int countPoint=0,px=0,py=0;
    for(int i=0;i<gameMap->col;i++)
    {
        for(int j=0;j<gameMap->row;j++)
        {
            int  inx=i*mywall.width();
            int  iny=j*mywall.height();
            if((inx >= newx && inx <= newx+mytank.width())&&(iny>=newy && iny <=newy+mytank.height()))
            {
                px=j;
                py=i;
                countPoint++;
            }
        }
    }
    switch (countPoint)
    {
    case 1:
    {
        if(gameMap->Gmap[px-1][py-1] == wall ||gameMap->Gmap[px][py-1] == wall ||gameMap->Gmap[px-1][py] == wall ||gameMap->Gmap[px][py] == wall)
            return true;
        else return false;
    }
    case 2:
    {
        if((px-1)*mywall.height()>=newy && (px-1)*mywall.height()<=newy+mytank.height())
        {
            if(gameMap->Gmap[px-1][py-1] == wall || gameMap->Gmap[px-1][py] == wall) return true;
            else return false;
        }
        else
        {
            if(gameMap->Gmap[px-1][py-1] == wall || gameMap->Gmap[px][py-1] == wall) return true;
            else return false;
        }
    }
    case 4:
    {
        if(gameMap->Gmap[px-1][py-1] == wall) return true;
        else return false;
    }
    default:
    {
        qDebug()<<"相撞判定错误！ 请重试";
        return true;
    }
    }
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if(event->isAutoRepeat())
    {
        switch(event->key())
        {
            case Qt::Key_W:
        {
            player1->mydir=xup;
            int i;
            for(i=0;!iscollide(0,-i)&& i<=player1->QuickmoveStep;i++);
            i--;
            player1->move(0,-i);
            break;
        }
        case Qt::Key_S:
        {
            player1->mydir=xdown;
            int i;
            for(i=0;!iscollide(0,i)&& i<=player1->QuickmoveStep;i++);
            i--;
            player1->move(0,i);
            break;
        }
        case Qt::Key_A:
        {
            player1->mydir=xleft;
            int i;
            for(i=0;!iscollide(-i,0)&& i<=player1->QuickmoveStep;i++);
            i--;
            player1->move(-i,0);
            break;
        }
        case Qt::Key_D:
        {
            player1->mydir=xright;
            int i;
            for(i=0;!iscollide(i,0)&& i<=player1->QuickmoveStep;i++);
            i--;
            player1->move(i,0);
            break;
        }
        }
        return;
    }
    switch(event->key())
    {
        case Qt::Key_W:
    {
        player1->mydir=xup;
        int i;
        for(i=0;!iscollide(0,-i)&& i<=player1->moveStep;i++);
        i--;
        player1->move(0,-i);
        break;
    }
    case Qt::Key_S:
    {
        player1->mydir=xdown;
        int i;
        for(i=0;!iscollide(0,i)&& i<=player1->moveStep;i++);
        i--;
        player1->move(0,i);
        break;
    }
    case Qt::Key_A:
    {
        player1->mydir=xleft;
        int i;
        for(i=0;!iscollide(-i,0) && i<=player1->moveStep;i++);
        i--;
        player1->move(-i,0);
        break;
    }
    case Qt::Key_D:
    {
        player1->mydir=xright;
        int i;
        for(i=0;!iscollide(i,0) && i<=player1->moveStep;i++);
        i--;
        player1->move(i,0);
        break;
    }
    }
}
